package teamproject.graphEditor.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;

import teamproject.graphEditor.GraphEditor;
import teamproject.graphEditor.PDEEdge;

public class RenamePDEEdgeDialog extends JDialog {

	private PDEEdge edge;
	private JLabel nameLabel;
	private JFormattedTextField nameTextfield;
	private JButton okButton;
	private GraphEditor graphEditor;

	public RenamePDEEdgeDialog(JFrame parent, PDEEdge edge,
			GraphEditor graphEditor) {
		super(parent, true);
		this.graphEditor = graphEditor;
		this.edge = edge;
		this.setTitle("Kante: " + edge.getName());
		initComponents();
		nameTextfield.setValue(edge.getName());
	}

	private void initComponents() {
		nameLabel = new JLabel(edge.getName());
		okButton = new JButton("OK");
		nameTextfield = new JFormattedTextField();

		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				okButtonHandler(e);
			}

		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.TRAILING)
								.add(okButton)
								.add(layout
										.createSequentialGroup()
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(nameLabel))
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(layout
														.createSequentialGroup()
														.addPreferredGap(
																org.jdesktop.layout.LayoutStyle.RELATED)
														.add(nameTextfield,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																69,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(nameLabel)
								.add(nameTextfield,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(okButton)));
		pack();
	}

	private void okButtonHandler(ActionEvent e) {
		edge.setName((String) nameTextfield.getValue());
		this.dispose();
		graphEditor.getVisualizationViewer().repaint();
	}
}
